/**
 * 
 */
package org.ranch.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * represents a concept in a system. e.g name can be cow,goat,hen together with
 * the description
 * 
 */
@Entity
@Table(name = "concepts")
public class Concept extends BaseData implements Comparable<Concept>, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * Name of concept.
     */
    private String name;
    /**
     * Categories to which this concept belongs.
     */
    private List<ConceptCategory> categories;
    /**
     * description of concept.
     */
    private String description;
    /**
     * concept code.
     */
    private String code;

    /**
     * default constructor.
     */
    public Concept() {
        super();
    }
    /**
     * constructor with initial specified values
     * @param conceptName {@link #name}
     * @param conceptCode {@link #code}
     * @param conceptCategories{@link #categories}
     * @param conceptDescription{@link #description}
     */

    public Concept(String conceptName, String conceptCode,
            List<ConceptCategory> conceptCategories, String conceptDescription) {
        super();
        this.name = conceptName;
        this.code = conceptCode;
        this.categories = conceptCategories;
        this.description = conceptDescription;
    }

    /**
     * Gets the name of the concept
     * @return the name
     */
    @Column(name="concept_name", nullable=false)
    public String getName() {
        return name;
    }

    /**
     * sets the name of the concept
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the list of concept categories
     * @return the categories
     */
    @ManyToMany
    @JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "concept_id",
	    "concept_category_id" }), name = "concept_category_link", joinColumns = @JoinColumn(
	    name = "concept_id", unique = false), inverseJoinColumns = @JoinColumn(
	    name = "concept_category_id", unique = false))
    public List<ConceptCategory> getCategories() {
        return categories;
    }

    /**
     * Sets the categories
     * @param categories
     *            the categories to set
     */
    public void setCategories(List<ConceptCategory> categories) {
        this.categories = categories;
    }

    /**
     *
     * Gets the concept descripion
     * @return the description
     */
    @Column(name = "concept_description", nullable = false)
    public String getDescription() {
        return description;
    }

    /**
     * Sets the concept description
     *
     * @param description
     *            the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Sets the unique code assigmed to this concept
     * @return the code
     */
    @Column(name = "concept_code", unique = true, nullable = true)
    public String getCode() {
        return code;
    }

    /**
     * Sets the unique code for the concept
     * @param code
     *            the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * Check whether this concept belongs to the given concept category
     * @param o
     * @return
     */
    @Override
    public int compareTo(Concept o) {
        return this.getName().compareToIgnoreCase(o.getName());
    }
}
